package org.x.user.controller;

import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;
import org.x.common.config.Result;
import org.x.common.enums.LogAsync;
import org.x.user.dto.*;

import org.x.user.service.SysUserService;

@RestController
@RequestMapping("/api/user")
public class SysUserController {

    @Autowired
    SysUserService sysUserService;


    @LogAsync("查询用户")
    @GetMapping("/list")
    public Result list(@ModelAttribute UserQueryDTO userQueryDTO) {
        return Result.ok(sysUserService.pageList(userQueryDTO));
    }


    @LogAsync("创建用户")
    ///api/user/create
    @PostMapping("/create")
    public Result create(@RequestBody @Valid CreateUserDTO createUserDTO) {
        int user = sysUserService.createUser(createUserDTO);
        return  Result.ok(user);
    }

    //修改用户
    @LogAsync("修改用户")
    @PutMapping("/update")
    public Result update(@RequestBody @Valid UpdateUserDTO updateUserDTO) {
        int user = sysUserService.updateUser(updateUserDTO);
        return  Result.ok(user);
    }

    @LogAsync("删除用户")
    @DeleteMapping("/delete")
    public Result delete(@RequestBody @Valid DeleteUserDTO deleteUserDTO){

        int user = sysUserService.deleteUser(deleteUserDTO);
        return Result.ok(user);
    }

    // 在 SysUserController.java 中添加
    @LogAsync("修改用户密码")
    @PutMapping("/updatePassword")
    public Result updatePassword(@RequestBody @Valid UpdateUserPasswordDTO updatePasswordDTO) {
        sysUserService.updateUserPassword(updatePasswordDTO);
        return Result.ok("密码修改成功");
    }



    @LogAsync("获取个人用户信息")
    @GetMapping("/profile")
    public Result profile() {
        // 调用服务层方法获取用户信息
        UserProfileDTO userProfileDto = sysUserService.getUserProfile();
        return Result.ok(userProfileDto);
    }


    @LogAsync("更新个人用户信息")
    @PutMapping("/profile")
    public Result updateProfile(@RequestBody @Valid UpdateProfileDTO updateProfileDTO) {
        // 调用服务层方法更新用户信息

        sysUserService.updateUserProfile(updateProfileDTO);
        return Result.ok();
    }

    @LogAsync("修改密码")
    @PutMapping("/password")
    public Result updatePassword(@RequestBody @Valid UpdatePasswordDTO updatePasswordDTO) {
        // 调用服务层方法修改密码
        sysUserService.updatePassword(updatePasswordDTO);
        return Result.ok("密码修改成功，请重新登录");
    }



    @LogAsync("更换绑定手机")
    @PutMapping("/phone")
    public Result updatePhone(@RequestBody @Valid UpdatePhoneDTO updatePhoneDTO) {
        // 调用服务层方法更换手机号
        sysUserService.updatePhone(updatePhoneDTO);
        return Result.ok("手机号更换成功");
    }

    @LogAsync("提交实名认证")
    @PostMapping("/auth")
    public Result authSubmit(@RequestBody @Valid AuthSubmitDTO authSubmitDTO) {
        sysUserService.submitAuth(authSubmitDTO);
        return Result.ok("实名认证已提交，等待审核");
    }


    // 在 SysUserController.java 中添加头像上传接口
    @LogAsync("上传头像")
    @PostMapping("/profile/avatar")
    public Result uploadAvatar(@RequestParam("file") MultipartFile file) {
        // 调用服务层方法上传头像
        String avatarUrl = sysUserService.uploadAvatar(file);
        return Result.ok( "头像上传成功");
    }





}



